package base;

/**
 * An orderItem contains one certain type of product and the quantity of user needs.
 * The class's objects will be added into an order's item list.
 */

public class OrderItem {
    private Product product;
    private int     quantity;

    public OrderItem(Product product, int quantity) {
        this.product = product;
        this.quantity = quantity;
    }

    public Product getProduct() {
        return this.product;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public int getQuantity() {
        return this.quantity;
    }

    public void addQuantity() {quantity += 1;}

    public void subQuantity() throws UnsupportedOperationException{
        if (quantity > 0) {
            quantity -= 1;
        } else {
            throw new UnsupportedOperationException();
        }
    }

    /**
     * to calculate the cost of purchasing the product
     * return the result
     */
    public double getValue() {
        return product.getPrice() * this.getQuantity();
    }

    //in Orderitem
    @Override
    public String toString() {
        return String.format("x%-7d|%-4s|%-6.2f|%-6.2f\n",getQuantity(), product.getCode(), product.getPrice(), getValue());
    }

}